import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:shop/router/router.dart';
import 'package:shop/util/color.dart';
import 'package:shop/views/search_view.dart';
import 'package:shop/vm/goods_vm.dart';

class GoodsPage extends StatefulWidget {
  final String arguments;
  const GoodsPage({super.key, required this.arguments});

  @override
  State<GoodsPage> createState() => _GoodsPageState();
}

class _GoodsPageState extends State<GoodsPage> {
  final viewModel = GoodsViewModel();

  @override
  void initState() {
    super.initState();

    viewModel.searchGoods(widget.arguments);
  }

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<GoodsViewModel>.value(
      value: viewModel,
      child: Scaffold(
        backgroundColor: Colors.white,
        appBar: AppBar(
            iconTheme: IconThemeData(color: Colors.white),
            title: Text('黑猫优购', style: TextStyle(color: Colors.white)),
            backgroundColor: HexColor.fromHex('#C00000')),
        body: Column(
          children: [
            SearchView(
              callback: (text) {
                viewModel.searchGoods(text);
              },
            ),
            Expanded(
              child: Consumer<GoodsViewModel>(builder: (context, value, child) {
                return ListView.separated(
                  keyboardDismissBehavior:
                      ScrollViewKeyboardDismissBehavior.onDrag,
                  padding: EdgeInsets.all(0),
                  itemCount: value.goodsList?.length ?? 0,
                  itemBuilder: (BuildContext context, int index) {
                    final goods = value.goodsList?[index];
                    return InkWell(
                      child: Container(
                        height: 120,
                        padding: EdgeInsets.all(10),
                        child: Row(
                          children: [
                            CachedNetworkImage(
                              width: 100,
                              imageUrl: goods?.goodsSmallLogo ?? '',
                              fit: BoxFit.cover,
                              errorWidget: (context, url, error) =>
                                  Icon(Icons.error),
                            ),
                            SizedBox(
                              width: 15,
                            ),
                            Expanded(
                                child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Text(
                                  goods?.goodsName ?? '',
                                  maxLines: 3,
                                  overflow: TextOverflow.ellipsis,
                                ),
                                Spacer(),
                                Text(
                                  '¥${goods?.goodsPrice}',
                                  textAlign: TextAlign.left,
                                  style: TextStyle(
                                      color: HexColor.fromHex('#C00000'),
                                      fontSize: 18),
                                ),
                              ],
                            )),
                          ],
                        ),
                      ),
                      onTap: () {
                        Navigator.pushNamed(context, detailPage,
                            arguments: '${goods?.goodsId}');
                      },
                    );
                  },
                  separatorBuilder: (context, index) {
                    return Divider(
                      height: 1,
                    );
                  },
                );
              }),
            )
          ],
        ),
      ),
    );
  }
}
